package com.example.lanouhn.firstapp.activity.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.adapter.CollectionGoodsAdapter;
import com.example.lanouhn.firstapp.activity.model.CollectionGoods;
import com.example.lanouhn.firstapp.activity.utils.CollectionGoodsJson;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;

import java.util.List;


//我的收藏商品页面
public class MyCollectionGoodsActivity extends AppCompatActivity {

    //用户登录标识
    private String key;
    private String goods_id;
    private RecyclerView recyclerView;
    //数据集合
    private List<CollectionGoods> CList;
    //适配器
    private CollectionGoodsAdapter adapter;
    //关于我们--跳转
    private TextView tv_aboutOur;
    //免责声明
    private TextView tv_disclaimer;
    //商务合作
    private TextView tv_cooperate;
    //帮助中心
    private TextView tv_help;
    //注销
    private TextView tv_cancellation;

    private TextView disclaimer;

    //注册广播接收器
    private MyBroadcastReceiver myBroadcastReceiver;

    private  ImageView iv_back;
    private TextView mineCenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collection_goods);
        key = (String) SPUtils.get(MyCollectionGoodsActivity.this,"log_key","");

        initView();
        //请求网络数据
        getDataFromWeb();
        //点击事件
        initEvent();

    }

    private void initEvent() {
        //关于我们--的点击事件
        tv_aboutOur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyCollectionGoodsActivity.this, AboutOurActivity.class);
                startActivity(intent);
            }
        });

        //免责声明--点击事件
        tv_disclaimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyCollectionGoodsActivity.this, DisclaimerActivity.class);
                startActivity(intent);
            }
        });
        //商务合作--点击事件
        tv_cooperate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyCollectionGoodsActivity.this, CooperateActivity.class);
                startActivity(intent);
            }
        });
        //帮助中心--点击事件
        tv_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyCollectionGoodsActivity.this, HelpActivity.class);
                startActivity(intent);
            }
        });
        //注销--点击事件
        tv_cancellation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyCollectionGoodsActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });

        //个人中心--点击事件
        mineCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    //内部类广播接收器
    class MyBroadcastReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            getDataFromWeb();
        }

    }

    //获取解析数据
    private void getDataFromWeb() {
        disclaimer.setVisibility(View.GONE);
        //开启线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://www.niaolema.com/mobile/index.php?act=member_favorites&op=favorites_list&page=undefined&curpage=undefined";
                String params = "key=" + key;
                String result = HttpUtils.doPost(url,params);
                if(!result.equals("") && !result.equals("null")){
                    CList = CollectionGoodsJson.getCollectionGoods(result);

                    if(CList.size()>0){
                        //解析成功
                        handler.sendEmptyMessage(1);
                    }else {
                        handler.sendEmptyMessage(0);
                    }


                }else {
                    handler.sendEmptyMessage(0);

                }


            }
        }).start();



    }

    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case 0:
                    disclaimer.setVisibility(View.VISIBLE);
                    break;
                case 1:

                    //创建adapter
                    adapter  = new CollectionGoodsAdapter(CList,MyCollectionGoodsActivity.this);
                    recyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    //点击跳转到商品详情
                    adapter.setCollectionGoodsRecyclerViewListener(new CollectionGoodsAdapter.CollectionGoodsRecyclerViewListener() {
                        @Override
                        public void onClick(View view, int position) {
                            Intent intent = new Intent(MyCollectionGoodsActivity.this,GoodsDetailsActivity.class);
                            intent.putExtra("goods_id",CList.get(position).getGoods_id());
                            startActivity(intent);

                        }
                    });

                    break;
                default:
                    break;


            }


        }
    };

    //找到控件
    private void initView() {

        mineCenter= (TextView) findViewById(R.id.mineCenter);
        iv_back= (ImageView) findViewById(R.id.iv_back);

        //暂无记录
        disclaimer = (TextView) findViewById(R.id.disclaimer);

        tv_aboutOur = (TextView) findViewById(R.id.mine_aboutOur);
        tv_disclaimer = (TextView) findViewById(R.id.mine_disclaimer);
        tv_cooperate = (TextView) findViewById(R.id.mine_cooperate);
        tv_help = (TextView) findViewById(R.id.mine_help);
        tv_cancellation = (TextView) findViewById(R.id.mine_cancellation);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //加载局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(gridLayoutManager);

        //动态注册广播接收器
        myBroadcastReceiver = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("delete");
        registerReceiver(myBroadcastReceiver,filter);




    }

    //注销广播
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadcastReceiver);
    }
}